package com.piz.web.product;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.piz.model.ItemDetails;
import com.piz.service.common.UnexpectedServiceException;
import com.piz.service.model.ItemBean;
import com.piz.service.productservice.ProductMgmtService;


@Controller
public class ProductListAction 
{
	
	
	@Autowired
	private ProductMgmtService productMgmtService;
	
	@RequestMapping(value = "productList" , method = RequestMethod.GET)
	public ModelAndView productList(@RequestParam(value = "classId",required = false) Long classId,
			 @RequestParam(value = "catId",required = false) Long catId , 
			@RequestParam(value = "tasteId",required = false) Long tasteId ,
			Model model, HttpServletRequest request)
	{
		List<ItemDetails> items;
		List<ItemBean> itemList;
		try {
			items = productMgmtService.fetchItemByCriteria(classId, catId, tasteId);
			itemList = productMgmtService.populateItemBean(items);
		} 
		catch (Exception e) {
			throw new UnexpectedServiceException("Exception in getting product list");
		}
		
		Map<String, Object> refrenceData = new HashMap<String, Object>();
		refrenceData.put("items", itemList);
		model.addAllAttributes(refrenceData);
		return new ModelAndView("admin/productList");
	}
	
	@RequestMapping(value = "deleteProduct" , method = RequestMethod.GET)
	public ModelAndView deleteProduct(@RequestParam(value="id", defaultValue = "0.0") Long id,Model model,HttpServletRequest request) 
	{
		productMgmtService.deleteProduct(id);
		return new ModelAndView("redirect: productList.action");
	}

}
